let myChart = {};

let datalist =[
  [0, 10452.74, 10409.85, 10367.41, 10554.96, 168890000],
  [1, 10411.85, 10544.07, 10411.85, 10575.92, 221290000],
  [2, 10543.85, 10538.66, 10454.37, 10584.07, 191460000],
  [3, 10535.46, 10529.03, 10432, 10587.55, 225490000],
  [4, 10530.07, 10592.44, 10480.59, 10651.99, 237770000],
  [5, 10589.25, 10458.89, 10420.52, 10603.48, 223250000],
  [6, 10461.55, 10485.18, 10389.85, 10543.03, 197960000],
  [7, 10485.18, 10427.18, 10341.19, 10539.25, 197310000],
  [8, 10428.67, 10538.37, 10426.89, 10573.85, 186280000],
  [9, 10534.52, 10553.85, 10454.52, 10639.03, 260090000],
  [10, 10556.37, 10600.51, 10503.7, 10666.88, 254170000],
  [11, 10601.4, 10528.66, 10447.92, 10676.96, 224300000],
  [12, 10522.77, 10623.62, 10453.11, 10665.7, 214920000],
  [13, 10624.22, 10623.18, 10545.03, 10717.4, 219720000],
  [14, 10625.25, 10568.29, 10490.14, 10691.77, 234260000],
  [15, 10568, 10702.51, 10510.44, 10725.18, 186170000],
  [16, 10701.1, 10609.92, 10579.33, 10748.81, 206560000],
  [17, 10610.07, 10468.37, 10412.44, 10703.25, 247660000],
  [18, 10467.41, 10510.29, 10369.92, 10611.56, 273970000],
  [19, 10510.22, 10488.07, 10385.56, 10551.03, 208990000],
  [20, 10487.78, 10499.18, 10395.55, 10614.44, 224800000],
  [21, 10499.48, 10505.18, 10414.15, 10571.48, 183810000],
  [22, 10503.11, 10470.74, 10394.81, 10567.85, 227760000],
  [23, 10469.33, 10495.55, 10399.92, 10566.37, 187810000],
  [24, 10494.89, 10593.03, 10433.7, 10634.81, 182880000],
  [25, 10592, 10579.03, 10433.7, 10634.81, 160720000],
  [26, 10578.74, 10613.85, 10511.18, 10667.03, 160590000],
  [27, 10605.48, 10737.7, 10561.55, 10779.4, 277850000],
  [28, 10735.18, 10694.07, 10636.44, 10775.03, 197560000],
  [29, 10696.22, 10627.85, 10578.66, 10755.47, 208340000],
  [30, 10628.88, 10714.88, 10628.88, 10762.07, 169730000],
  [31, 10706.68, 10671.99, 10623.62, 10764.36, 164370000],
  [32, 10674.59, 10664.73, 10626.44, 10794.95, 219890000],
  [33, 10666.29, 10619.03, 10559.11, 10722.77, 220560000],
  [34, 10619.55, 10609.62, 10508.89, 10711.84, 229950000],
  [35, 10609.55, 10566.37, 10479.33, 10681.4, 225670000],
  [36, 10566.59, 10601.62, 10509.4, 10660.73, 192420000],
  [37, 10598.14, 10580.14, 10493.7, 10652.96, 223230000],
  [38, 10581.55, 10583.92, 10519.03, 10689.55, 200050000],
  [39, 10582.25, 10678.14, 10568.74, 10720.14, 185030000],
  [40, 10678.36, 10591.48, 10539.4, 10713.92, 215580000],
  [41, 10588.59, 10593.11, 10506.66, 10651.03, 188800000],
  [42, 10593.48, 10588, 10522.59, 10645.33, 161050000],
  [43, 10582.59, 10595.55, 10497.11, 10681.4, 223550000],
  [44, 10595.37, 10529.48, 10505.85, 10677.85, 199300000],
  [45, 10529.52, 10456.96, 10391.48, 10567.03, 246270000],
  [46, 10457.59, 10296.89, 10259.34, 10523.11, 259000000],
  [47, 10288.85, 10128.38, 10102.75, 10356.22, 292050000],
  [48, 10130.67, 10240.08, 10097.04, 10281.63, 223350000],
  [49, 10238.45, 10102.89, 10066.08, 10252.68, 219150000],
  [50, 10103.41, 10184.67, 10085.34, 10253.26, 194560000],
  [51, 10184.3, 10300.3, 10184.3, 10356.59, 181210000],
  [52, 10298.96, 10295.78, 10187.78, 10355.04, 218820000],
  [53, 10295.85, 10186.6, 10163.71, 10355.41, 261590000],
  [54, 10185.93, 10064.75, 9985.19, 10185.93, 248930000],
  [55, 10066.67, 10063.64, 10020.75, 10177.04, 215260000],
  [56, 10065.41, 10048.23, 9975.86, 10140.23, 224310000],
  [57, 10049.56, 10218.82, 10049.56, 10246.15, 216420000],
  [58, 10218.37, 10212.97, 10145.63, 10306.22, 198830000],
  [59, 10212.91, 10329.63, 10212.91, 10389.93, 197150000],
  [60, 10327.63, 10381.7, 10264.15, 10411.41, 189060000],
  [61, 10380.89, 10357.7, 10287.11, 10428.59, 207400000],
  [62, 10357.52, 10373.33, 10299.48, 10449.33, 218660000],
  [63, 10375.33, 10470.59, 10375.33, 10548.74, 243070000],
  [64, 10470.59, 10558.37, 10423.33, 10582.22, 182130000],
  [65, 10553.76, 10570.81, 10467.26, 10596.37, 175720000],
  [66, 10569.26, 10480.15, 10422.74, 10580.51, 218040000],
  [67, 10482.77, 10442.03, 10383.84, 10590.15, 187730000],
  [68, 10444.38, 10515.56, 10439.27, 10559.28, 142190000],
  [69, 10516.05, 10381.28, 10343.17, 10572.13, 202540000],
  [70, 10378.1, 10377.95, 10259.35, 10453.39, 230460000],
  [71, 10377.95, 10397.46, 10279.37, 10481.21, 262880000],
  [72, 10398.32, 10451.97, 10343.74, 10500.57, 234660000],
  [73, 10451.62, 10437.85, 10351.97, 10501.79, 173340000],
  [74, 10437.85, 10314.5, 10297.39, 10530.61, 204710000],
  [75, 10311.87, 10317.27, 10200.38, 10398.53, 232630000],
  [76, 10314.99, 10461.2, 10255.88, 10529.12, 265740000],
  [77, 10463.11, 10472.84, 10362.97, 10543.95, 277070000],
  [78, 10472.91, 10444.73, 10396.75, 10540.26, 183040000],
  [79, 10445.38, 10478.16, 10410.52, 10570.92, 213410000],
  [80, 10476.67, 10342.6, 10301.65, 10479.58, 232090000],
  [81, 10339.41, 10272.27, 10199.31, 10443.81, 231880000],
  [82, 10273.06, 10225.57, 10198.39, 10374.61, 218010000],
  [83, 10227.27, 10314, 10199.67, 10365.74, 198830000],
  [84, 10314.32, 10317.2, 10232.31, 10403.14, 208020000],
  [85, 10316.98, 10310.95, 10249.63, 10382.98, 172170000],
  [86, 10308.2, 10241.26, 10147.21, 10332.1, 202690000],
  [87, 10240.62, 10117.34, 10086.94, 10302.93, 228140000],
  [88, 10116.28, 9990.02, 9881.86, 10116.28, 272130000],
  [89, 9989.24, 10019.47, 9928.91, 10092.78, 223650000],
  [90, 10011.52, 10045.16, 9822.1, 10089.87, 246410000],
  [91, 10044.31, 10010.74, 9924.94, 10100.24, 215300000],
  [92, 10008.43, 10012.87, 9912.45, 10096.69, 175180000],
  [93, 10009.92, 9906.91, 9827.21, 10009.92, 199660000],
  [94, 9906.71, 9968.51, 9895.77, 10028.27, 191770000],
  [95, 9962.55, 9937.71, 9919.9, 10124.79, 227420000],
  [96, 9939.12, 9937.64, 9867.73, 10014.5, 155570000],
  [97, 9939.34, 9966.74, 9910.81, 10058.5, 180980000],
  [98, 9968.02, 9958.43, 9891.22, 10084.91, 187000000],
[99, 9958.08, 10117.62, 9895.41, 10139.27, 213950000]
]

/* 处理数据 */

function splitData(rawData) {
    var categoryData = [];
    var values = [];
    for (var i = 0; i < rawData.length; i++) {
        categoryData.push(rawData[i][0]);
        rawData[i][0] = i;
        values.push(rawData[i]);
    }
    return {
        categoryData: categoryData,
        values: values
    };
}

function renderItem(params, api) {
  var xValue = api.value(0);
  var openPoint = api.coord([xValue, api.value(1)]);
  var closePoint = api.coord([xValue, api.value(2)]);
  var lowPoint = api.coord([xValue, api.value(3)]);
  var highPoint = api.coord([xValue, api.value(4)]);
  var halfWidth = api.size([1, 0])[0] * 0.35;
  var style = api.style({
    stroke: api.visual('color')
  });

  return {
    type: 'group',
    children: [{
      type: 'line',
      shape: {
        x1: lowPoint[0],
        y1: lowPoint[1],
        x2: highPoint[0],
        y2: highPoint[1]
      },
      style: style
    }, {
      type: 'line',
      shape: {
        x1: openPoint[0],
        y1: openPoint[1],
        x2: openPoint[0] - halfWidth,
        y2: openPoint[1]
      },
      style: style
    }, {
      type: 'line',
      shape: {
        x1: closePoint[0],
        y1: closePoint[1],
        x2: closePoint[0] + halfWidth,
        y2: closePoint[1]
      },
      style: style
    }]
  };
}

function fenshi(idName, infolist) {
  let echarts = require('echarts'),
    main1 = document.getElementById(idName);
  let parent = $(main1.parentNode);
  main1.style.width = parent.width() + 'px';
  main1.style.height = parent.height() + 'px';

  console.log(main1);
  debugger;

  myChart = echarts.init(main1);
  // infolist = datalist;
  var data = splitData(infolist);
  let option = {
    backgroundColor: '#000',
    animation: false,
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross'
      },
      backgroundColor: 'rgba(245, 245, 245, 0.8)',
      borderWidth: 1,
      borderColor: '#ccc',
      padding: 10,
      textStyle: {
        color: '#000'
      },
      position: function(pos, params, el, elRect, size) {
        var obj = {
          top: 10
        };
        obj[['left', 'right'][+(pos[0] < size.viewSize[0] / 2)]] = 30;
        return obj;
      },
      extraCssText: 'width: 170px'
    },
    axisPointer: {
      link: {
        xAxisIndex: 'all'
      },
      label: {
        backgroundColor: '#777'
      }
    },
    toolbox: {
      feature: {
        dataZoom: {
          yAxisIndex: false
        },
        brush: {
          type: ['lineX', 'clear']
        }
      }
    },
    grid: [{
      left: '10%',
      right: '8%',
      bottom: 150
    }],
    xAxis: [{
      type: 'category',
      data: data.categoryData,
      scale: true,
      boundaryGap: false,
      axisLine: {
        onZero: false,
        lineStyle: {
          color: '#fff',
        }
      },
      axisLabel: {
        show: true,
        textStyle: {
          color: '#eee'
        }
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'dashed' //设置网格线类型 dotted：虚线   solid:实线
        }
      },
      splitNumber: 20,
      min: 'dataMin',
      max: 'dataMax',
      axisPointer: {
        z: 100
      }
    }],
    yAxis: [{
      scale: true,
      position: 'right',
      splitLine: {
        show: true,
        lineStyle: {
          type: 'dashed' //设置网格线类型 dotted：虚线   solid:实线
        },
      },
      axisLabel: {
        show: true,
        textStyle: {
          color: '#fff'
        }
      },
      axisLine: {
        lineStyle: {
          color: '#fff',
        }
      },
    }],
    dataZoom: [{
        type: 'inside',
        start: 98,
        end: 100,
        minValueSpan: 10
      },
      {
        show: false,
        type: 'slider',
        bottom: 60,
        start: 98,
        end: 100,
        minValueSpan: 10
      }
    ],
    series: [{
      name: 'Dow-Jones index',
      type: 'custom',
      renderItem: renderItem,
      dimensions: [null, 'open', 'close', 'lowest', 'highest'],
      encode: {
        x: 0,
        y: [1, 2, 3, 4],
        tooltip: [1, 2, 3, 4]
      },
      data: data.values,
      itemStyle: {
        normal: {
          color: '#00ff00'
        }
      }
    }]
  };
  myChart.setOption(option);
}

export {
  fenshi
};
